<?php

namespace catchAdmin\business\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\business\model\Airlift as AirliftModel;
use think\Response;
use catcher\CatchAuth;
use think\facade\Db;
class Airlift extends CatchController
{
    
    protected $airliftModel;
    
    /**
     *
     * @time 2022/02/15 11:41
     * @param AirliftModel $airliftModel
     * @return mixed
     */
    public function __construct(AirliftModel $airliftModel)
    {
        $this->airliftModel = $airliftModel;
    }
    
    /**
     *
     * @time 2022/02/15 11:41
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function index(CatchAuth $auth)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->airliftModel->getList($user['tenant_id']));
    }
    
    /**
     *
     * @time 2022/02/15 11:41
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function create(CatchAuth $auth,Request $request)
    {
         $params = $request->filterEmptyField()->post();
         Db::startTrans();
         try {
             $user = $auth->user();
             // 基本信息
             if(!empty($params['basic'])) {
                 $params['basic']['job_number'] = 'AE'.date('YmdHis');
                 $params['basic']['tenant_id'] = $user['tenant_id'];
                 $params['basic']['business_type'] = 'AE';
                 $params['basic']['creator_id'] = $user['id'];
                 $this->airliftModel->storeBy($params['basic']);
             } else {
                 return CatchResponse::fail('新建失败');
             }

             //其他信息
             if(!empty($params['otherinfo'])) {
                  $data['datas'] = json_encode($params['otherinfo']);
                  $data['job_number'] = $params['basic']['job_number'];
                  $data['tenant_id'] = $user['tenant_id'];
                  $data['created_at'] = time();
                  $data['updated_at'] = time();
                  Db::table('db_otherinfo')->insert($data);
             }

              //报关单证
             if(!empty($params['declaration'])) {
                  $params['declaration']['job_number'] =  $params['basic']['job_number'];
                  $params['declaration']['tenant_id'] = $user['tenant_id'];
                  $params['declaration']['created_at'] = time();
                  $params['declaration']['updated_at'] = time();
                  DB::name('db_declare_document')->insert($params['declaration']);
             }

             //运输明细
             if(!empty($params['transport'])) {
                 foreach ($params['transport'] as $key => $value){
                      unset($value['key']);
                      $value['job_number'] =  $params['basic']['job_number'];
                      $value['tenant_id'] = $user['tenant_id'];
                      $value['created_at'] = time();
                      $value['updated_at'] = time();
                      DB::name('db_transport')->insert($value);
                 }
             }
             Db::commit();
             return CatchResponse::success('', '新建成功');
        } catch (\Exception $exception) {
             Db::rollback();
             return CatchResponse::fail('新建失败');
        }
    }
    /**
     *
     * @time 2022/02/15 11:41
     * @param Request $request
     * @return Response
     */
    public function update(CatchAuth $auth,Request $request)
    {
         $params = $request->filterEmptyField()->post();
         Db::startTrans();
         try {
              //基本信息
             if(!empty($params['basic'])) {
                 $this->airliftModel->updateBy($params['basic']['id'],$params['basic']);
             } else {
                 return CatchResponse::fail('编辑失败');
             }

             //其他信息
             if(!empty($params['otherinfo'])) {
                 $data['datas'] = json_encode($params['otherinfo']);
                 $data['updated_at'] = time();
                 $data['job_number']=$params['basic']['job_number'];
                 if(empty($params['otherinfo']['id'])){
                     Db::table('db_otherinfo')->insert($data);
                 }else{
                     $id =  $params['otherinfo']['id'];
                     unset($params['otherinfo']['id']);
                     Db::table('db_otherinfo')->where('id', $id)->update($data);
                 }
             }

             //报关单
             if(!empty($params['declaration'])) {
                  $params['declaration']['updated_at'] = time();
                  if(empty($params['declaration']['id'])){
                      $params['declaration']['created_at'] = time();
                      $params['declaration']['updated_at'] = time();
                      $params['declaration']['job_number']=$params['basic']['job_number'];
                      DB::name('db_declare_document')->insert($params['declaration']);
                  }else{
                      DB::name('db_declare_document')->where('id', $params['declaration']['id'])->update($params['declaration']);
                  }
             }

             //运输明细
             if(!empty($params['transport'])) {
                 foreach ($params['transport'] as $key => $value) {
                     unset($value['key']);
                     $value['updated_at'] = time();
                     if(!empty($value['id'])) {
                         unset($value['transport_company_name']);
                         DB::name('db_transport')->where('id', $value['id'])->update($value);
                     } else {
                         $user = $auth->user();
                         $value['tenant_id'] = $user['tenant_id'];
                         $value['created_at'] = time();
                         $value['updated_at'] = time();
                         $value['job_number'] = $params['basic']['job_number'];
                         DB::name('db_transport')->insert($value);
                     }
                 }
             }
             Db::commit();
             return CatchResponse::success('', '编辑成功');
        } catch (\Exception $exception) {
             echo $exception;
             Db::rollback();
             return CatchResponse::fail('编辑失败');
        }
    }
    
    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
        $id = $request->post('id');
         Db::startTrans();
         try {
             $job_number = $this->airliftModel->where('id',$id)->value('job_number');
             $data['deleted_at'] = time();
             Db::name('db_declare_document')->where('job_number',$job_number)->update($data);
             Db::name('db_transport')->where('job_number','in',$job_number)->update($data);
             Db::table('db_otherinfo')->where('job_number',$job_number)->update($data);

             $this->airliftModel->deleteBy($id);
             Db::commit();
             return CatchResponse::success('', '删除成功');
        } catch (\Exception $exception) {
             Db::rollback();
             return CatchResponse::fail('删除失败');
        }
    }
    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function  getJobNumber(Request $request){
         $data = $this->airliftModel->column('job_number');
         return CatchResponse::success($data, '查询成功');
    }

    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function  getJobNumberInfo(Request $request){
         $job_number = $request->post('job_number');
         $where = [];
         $where[] = ['job_number','=',$job_number];
         $where[] = ['deleted_at','=',0];
         $data = $this->airliftModel->where($where)->field('entrust_date,volume,inside_type,main_doc_no,sub_order_No,entrust_company')->find();
         $data['entrust_company_info'] = DB::name('db_provider')->where('id',$data['entrust_company'])->find();
         $data['inside_type_name'] = DB::name('db_inside_type')->where('id',$data['inside_type'])->value('name');
         return CatchResponse::success($data, '查询成功');
    }

    /**
     * 打印
     * @param Request $request
     * @return \think\response\Json
     */
    public function print(Request $request){
        $id=$request->filterEmptyField()->post('id');
        //以id获取一条空进空出信息
        $data=$this->airliftModel->where('id',$id)->find();
        //以工作号获取运输明细数组
        $transports=Db::name('db_transport')->where('job_number','in',$data['job_number'])->select();
        //以创建人id获取用户名
        $creator=Db::name('users')->field('username')->where('creator_id',$data['creator_id'])->find();
        //获取委托单位名称
        $accountname=Db::name('db_provider')->field('account_name')->where('id',$data['entrust_company'])->find();
        //获取经营单位名称
        $accountname1=Db::name('db_provider')->field('account_name')->where('id',$data['management_company'])->find();
        //获取内部类型
        $insidetype=Db::name('db_inside_type')->field('name')->where('id',$data['inside_type'])->find();
        $data['inside_type']=$insidetype['name'];
        $data['management_company']=$accountname1['account_name'];
        $data['entrust_company']=$accountname['account_name'];
        $data['transports']=$transports;
        $data['creator']=$creator['username'];
        return CatchResponse::success($data,'查询成功');
    }

}